(ns pe.p35
  (:use pe.digits)
  (:use pe.primes)
  (:use pe.seq)
  (:use clojure.contrib.math)
  (:use clojure.contrib.combinatorics))

(defn rotations [digs]
  (let [c (cycle digs) cnt (count digs)]
    (take cnt (partition cnt 1 c))))

(defn make-number [digs]
  (let [with-index (re-indexed digs)]
    (loop [[index digit] (first with-index) ndigs (next with-index) num 0]
      (if (nil? index) num
        (recur (first ndigs) (next ndigs) (+ num (* digit (expt 10 (dec index)))))))))

(defn circular-prime? [n]
    (println n)
    (if (prime? n)
      (every? prime? (map make-number (rotations (digits n))))
      false))

(println (count (filter circular-prime? (range 1 1000000))))